﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：表单权限字段导入
 */


var acceptClick;
var auditorName = '';
var fieldName = '';
var formName = '';

var dbId = '';
var bootstrap = function ($, learun) {
    "use strict";

    var page = {
        init: function () {
            page.bind();
        },
        bind: function () {
            $('#AuditorFieldId').lrselect({
                value: 'columnName',
                text: 'columnName',
                title: 'columnComment',
                allowSearch: true,
                select: function (item) {
                    if (item) {
                        fieldName = item.f_column;
                    }
                    else {
                        fieldName = '';
                    }
                }
            });
            $('#ReFieldId').lrselect({
                value: 'columnName',
                text: 'columnName',
                title: 'columnComment',
                allowSearch: true
            });
            $('#ReFormId').lrselect({
                value: 'tableName',
                text: 'tableName',
                title: 'description',
                allowSearch: true,
                select: function (item) {
                    if (item) {
                        learun.httpAsync('GET', config.base_server() + 'dbTables/listFields?access_token=' + config.getToken().access_token, { dbKey: dbId, tableName: item.tableName }, function (data) {
                            if (data) {
                                $('#ReFieldId').lrselectRefresh({
                                    data: data
                                });
                                $('#AuditorFieldId').lrselectRefresh({
                                    data: data
                                });
                            }
                        });
                        formName = item.tableName;
                    }
                    else {
                        formName = '';
                        $('#ReFieldId').lrselectRefresh({
                            data: []
                        });
                        $('#AuditorFieldId').lrselectRefresh({
                            data: []
                        });
                    }

                }
            });
            $('#DbId').lrselect({
                url: config.base_server() + 'dbLinks/tree?access_token=' + config.getToken().access_token,
                type: 'tree',
                placeholder: '请选择数据库',
                allowSearch: true,
                select: function (item) {
                    if (item && !item.hasChildren) {
                        dbId = item.id;
                        $('#ReFormId').lrselectRefresh({
                            url: config.base_server() + 'dbTables?access_token=' + config.getToken().access_token,
                            param: { dbKey: item.id }
                        });
                    }
                    else {
                        dbId = '';
                        $('#ReFormId').lrselectRefresh({
                            url: false,
                            data: [],
                            param: {}
                        });
                    }
                }
            });
        }
    };
    // 保存数据
    acceptClick = function (callBack) {
        if (!$('#form').lrValidform()) {
            return false;
        }
        var formData = $('#form').lrGetFormData();

        var res = {};
        res.auditorId = formData.DbId + "|" + formData.ReFormId + "|" + formData.ReFieldId + "|" + formData.AuditorFieldId;
        res.auditorName = "【" + formName + "】" + fieldName;
        res.type = '5';//审核者类型1.岗位2.角色3.用户4.上下级5.表单指定字段6.某一个节点执行人
        callBack(res);
        return true;
    };
    page.init();
}